package com.xiaxueqi.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.dozer.DozerBeanMapperSingletonWrapper;
import org.dozer.Mapper;
import org.springframework.beans.factory.annotation.Autowired;

import com.niagara.dao.Page;
import com.niagara.service.base.BaseService;
import com.xiaxueqi.entity.security.AppUser;
import com.xiaxueqi.entity.security.AppUserDetail;
import com.xiaxueqi.entity.security.AppUserExt;
import com.xiaxueqi.service.UserDTOService;

public class UserDTOServiceImpl implements UserDTOService {

	@Autowired
	private BaseService baseService;

	public AppUserExt getAppUserDTO(AppUser appUser) throws Exception {
		AppUserExt user = new AppUserExt();
		if (appUser != null) {
			AppUserDetail userDetail = this.baseService.get(
					AppUserDetail.class, appUser.getId());
			// 相同的字段名就取appUser中的
			Mapper mapper = DozerBeanMapperSingletonWrapper.getInstance();
			mapper.map(userDetail, user);
			mapper.map(appUser, user);
		}
		return user;
	}

	public AppUserExt getAppUserDTOByLoginId(Integer loginId) throws Exception {
		AppUser appUser = this.baseService.get(AppUser.class, loginId);
		AppUserExt user = this.getAppUserDTO(appUser);
		return user;
	}

	public List<AppUserExt> getAppUserDTOList(List<AppUser> appUserList)
			throws Exception {
		List<AppUserExt> userList = new ArrayList<AppUserExt>();
		if (appUserList != null && appUserList.size() != 0) {
			for (AppUser appUser : appUserList) {
				AppUserExt user = this.getAppUserDTO(appUser);
				userList.add(user);
			}
		}
		return userList;
	}

	public Page<AppUserExt> getAppUserDTOPage(Page<AppUser> appUserPage)
			throws Exception {
		if (appUserPage == null)
			return null;
		else {
			List<AppUserExt> userList = this.getAppUserDTOList(appUserPage
					.getPageList());
			int currentPage = appUserPage.getCurrentPage();
			int pageSize = appUserPage.getPageSize();
			int totalCount = appUserPage.getTotalCount();
			Page<AppUserExt> userPage = new Page<AppUserExt>(currentPage,
					pageSize, totalCount, userList);
			return userPage;
		}
	}

}
